package com.googlecode.uipipedream.spec;

import java.awt.*;
import java.util.Observable;

public class ColourModel extends Observable {
	private float brightness = (float) 1.0;
	private float hue = (float) 0.0;
	private float saturation = (float) 0.0;
	
	private float offset = 1f/4;
	private Color color;
	
	public void setColor(Color c) { setRGB(c.getRed(), c.getGreen(), c.getBlue()); }
	public Color getColor() { return color; }
	
	// Get colours selection.
	public float getHue() { return hue; }
	public float getSaturation() { return saturation; }
	public float getBrightness() { return brightness; }
	
	// Suggested colours.
	public float getComplementary() { return (1f/2 + hue) % 1f; }
	public float getTriad1() { return (1f/3 + hue) % 1f; }
	public float getTriad2() { return ((1f/3) * 2 + hue) % 1f; }
	public float getAnalog1() { return (hue + offset) % 1f; }
	public float getAnalog2() { return (hue + 1f - offset) % 1f; }
	public float getSplitComplementary1() { return (getComplementary() + offset) % 1f; }
	public float getSplitComplementary2() { return (getComplementary() + 1f - offset) % 1f; }
	
	public void setBrightness(float brightness) {
		// Normalise input data.
		if (brightness < 0.0) brightness = 0f;
		if (brightness >= 1.0) brightness = 1f;
		
		// Set.
		this.brightness = brightness;
		color = Color.getHSBColor(hue, saturation, brightness);
		setChanged();
	}
	
	public void setHue(float hue) {
		// Normalise input data.
		if (hue < 0.0) hue = 0f;
		if (hue >= 1.0) hue = 0.999f;
		
		// Set.
		this.hue = hue;
		color = Color.getHSBColor(hue, saturation, brightness);
		setChanged();
	}
	
	public void setSaturation(float saturation) {
		// Normalise input data.
		if (saturation < 0.0) saturation = 0f;
		if (saturation >= 1.0) saturation = 1f;
		
		// Set.
		this.saturation = saturation;
		color = Color.getHSBColor(hue, saturation, brightness);
		setChanged();
	}
	
	public void setRGB(int r, int g, int b) {
		// Normalise.
		if (r < 0) r = 0;
		if (r > 255) r = 255;
		if (g < 0) g = 0;
		if (g > 255) g = 255;
		if (b < 0) b = 0;
		if (b > 255) b = 255;
		
		float[] vals = new float[3];
		Color.RGBtoHSB(r, g, b, vals);
		this.hue = vals[0];
		this.saturation = vals[1];
		this.brightness = vals[2];
		this.color = new Color(r, g, b);
		setChanged();
	}
	
	protected void setChanged() {
		super.setChanged();
		notifyObservers();
	}
}